﻿using Scrat.Core.Common;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Scrat.Teacher.Forms
{
    public partial class FServerConfig : UIEditForm
    {
        private static int _defaultLocalMessagePort = 9988;
        private static int _defaultLocalScreenSharingPort = 2333;

        private static int _defalutRemoteMessagePort = 8899;
        private static int _defalutRemoteScreenSharingPort = 2333;

        private ServerConfig _serverConfig;
        private List<string> IPv4Addresses;

        public ServerConfig ServerConfig 
        {
            get
            {
                ServerConfig temp = _serverConfig;

                temp.LocalServerAddress = cbLocalServerAddress.Text.Trim();
                temp.LocalMessagePort = int.Parse(txtLocalMessageServerPort.Text.Trim());
                temp.LocalScreenSharingPort = int.Parse(txtLocalScreenSharingPort.Text.Trim());

                temp.RemoteMessagePort = int.Parse(txtStuMessagePort.Text.Trim());
                temp.RemoteScreenSharingPort = int.Parse(txtStuScreenSharingPort.Text.Trim());

                return temp;
            }

            set
            {
                ServerConfig temp = value;
                ServerConfig serverConfig = new ServerConfig();
                serverConfig.SetLocalServerParam(temp.LocalServerAddress,
                    LimitPort(temp.LocalMessagePort, _defaultLocalMessagePort),
                    LimitPort(temp.LocalScreenSharingPort, _defaultLocalScreenSharingPort));
                serverConfig.SetRemoteServerParam(
                    LimitPort(temp.RemoteMessagePort, _defalutRemoteMessagePort),
                    LimitPort(temp.RemoteScreenSharingPort, _defalutRemoteScreenSharingPort));
                _serverConfig = serverConfig;
            }
        }
        public FServerConfig(ServerConfig serverConfig)
        {
            InitializeComponent();
            IPv4Addresses = NICUtil.GetAllIPv4Address();
            ServerConfig = serverConfig;
            RenderView();
        }

        private void RenderView()
        {
            // 本机服务参数配置
            cbLocalServerAddress.Items.Clear();
            cbLocalServerAddress.Items.AddRange(IPv4Addresses.ToArray());
            bool hasIt = false;
            for (int i = 0; i < IPv4Addresses.Count; i++)
            {
                if (_serverConfig.LocalServerAddress == IPv4Addresses[i])
                {
                    cbLocalServerAddress.SelectedIndex = i;
                    hasIt = true;
                }
            }
            if (!hasIt)
            {
                // 如果第一次传递的地址本机并没有, 那么默认选中最后一个,并将值改为最后一个网卡的IP地址信息
                cbLocalServerAddress.SelectedIndex = IPv4Addresses.Count - 1;
                _serverConfig.LocalServerAddress = IPv4Addresses[IPv4Addresses.Count - 1];
            }
            txtLocalMessageServerPort.Text = _serverConfig.LocalMessagePort.ToString();
            txtLocalScreenSharingPort.Text = _serverConfig.LocalScreenSharingPort.ToString();

            // 远端服务参数配置
            txtStuMessagePort.Text = _serverConfig.RemoteMessagePort.ToString();
            txtStuScreenSharingPort.Text = _serverConfig.RemoteScreenSharingPort.ToString();
        }

        /// <summary>
        /// 0 不使用
        /// 1 ~ 1023 为系统端口,又称为BSD保留端口
        /// 1024 ~ 65535为用户端口,其中又分为:
        ///     - BSD临时端口: 1024 ~ 5000, 一般的应用程序使用1024到4999来进行通讯
        ///     - BSD服务器(非特权): 5001 ~ 65535, 用户自定义
        /// </summary>
        /// <param name="port"></param>
        /// <param name="defaultPort"></param>
        /// <returns> 1024 ~ 65535 </returns>
        private int LimitPort(int port, int defaultPort)
        {
            return port > 1024 && port < 65535 ? port : defaultPort;
        }
    }

    public class ServerConfig
    {
        public string LocalServerAddress;
        public int LocalMessagePort;
        public int LocalScreenSharingPort;

        public int RemoteMessagePort;
        public int RemoteScreenSharingPort;

        /// <summary>
        /// 如果调用无参构造,则可以使用 SetLocalServerParam 与 SetRemoteServerParam 方便的为配置参数赋值
        /// </summary>
        public ServerConfig() { }

        public ServerConfig(string localServerAddress, int localMessagePort, int localScreenSharingPort, int remoteMessagePort, int remoteScreenSharingPort)
        {
            LocalServerAddress = localServerAddress;
            LocalMessagePort = localMessagePort;
            LocalScreenSharingPort = localScreenSharingPort;
            RemoteMessagePort = remoteMessagePort;
            RemoteScreenSharingPort = remoteScreenSharingPort;
        }

        public void SetLocalServerParam(string serverAddress, int messagePort, int screenSharingPort) 
        {
            LocalServerAddress = serverAddress;
            LocalMessagePort = messagePort;
            LocalScreenSharingPort = screenSharingPort;
        }

        public void SetRemoteServerParam(int messagePort, int screenSharingPort)
        {
            RemoteMessagePort = messagePort;
            RemoteScreenSharingPort = screenSharingPort;
        }
    }
}
